import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import Login from "@/views/Login/LoginPage.vue";
import Register from "@/views/Login/RegisterPage.vue";
import ForgotPassword from "@/views/Login/ForgotPassword.vue";
import Verification from "@/views/Login/VerificationCode.vue";
import RegisterVerification from "@/views/Login/RegisterVerificationCode.vue";
import ResetPassword from "@/views/Login/ResetPassword.vue";
import PasswordChange from "@/views/Login/PasswordChange.vue";
import VerificationSuccess from "@/views/Login/VerificationSuccess.vue";
import layout from "@/views/Main.vue";

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: '*', redirect: '/login' },
    { path: "/login", name: "login", component: Login },
    { path: "/register", name: "register", component: Register },
    { path: "/forgot", name: "forgot", component: ForgotPassword },
    { path: "/verification", name: "verification", component: Verification },
    { path: "/resetPassword", name: "resetPassword", component: ResetPassword },
    { path: "/passwordChange", name: "passwordChange", component: PasswordChange
    },
    { path: "/RegisterVerification", name: "registerVerification", component: RegisterVerification },
    { path: "/VerificationSuccess", name: "verificationSuccess", component: VerificationSuccess
    },
    {
      path: "/",
      name: "布局",
      component: layout,
      children: [
        {
          path: "home",
          name: "Product",
          component: () =>
            import(/* webpackChunkName: "home" */ "../views/Home/Product.vue"),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "personInformation",
          name: "personInformation",
          component: () =>
            import(
              /* webpackChunkName: "from" */ "../views/Home/PersonInformation.vue"
            ),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "userInformation",
          name: "userInformation",
          component: () =>
            import(
              /* webpackChunkName: "from" */ "../views/Home/UserInformation.vue"
            ),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "adminInformation",
          name: "adminInformation",
          component: () =>
            import(
              /* webpackChunkName: "from" */ "../views/Home/AdminInformation.vue"
            ),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "salesData",
          name: "salesData",
          component: () =>
            import(/* webpackChunkName: "from" */ "../views/Home/SalesData.vue"),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "salesDataDetail",
          name: "salesDataDetail",
          component: () =>
            import(
              /* webpackChunkName: "from" */ "../views/Home/SalesDataDetail.vue"
            ),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
        {
          path: "user",
          name: "user",
          component: () =>
            import(/* webpackChunkName: "from" */ "../views/Home/User.vue"),
          meta: {
            title: '',
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        },
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    const token = localStorage.getItem("satoken");
    if (token === null || token === "") {
      Vue.prototype.$message.error("please login!");
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
